package test.util;

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//


import java.security.SecureRandom;
import java.util.Random;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import org.apache.commons.lang3.StringUtils;

public class DESUtil {
    private static final String KEY = "kimwaynet";
    private static final String DES = "DES";
    private static Random randGen = null;
    private static char[] numbersAndLetters = null;

    public DESUtil() {
    }

    private static byte[] encrypt(byte[] src, byte[] key) throws Exception {
        SecureRandom sr = new SecureRandom();
        DESKeySpec dks = new DESKeySpec(key);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey securekey = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(1, securekey, sr);
        return cipher.doFinal(src);
    }

    private static byte[] decrypt(byte[] src, byte[] key) throws Exception {
        SecureRandom sr = new SecureRandom();
        DESKeySpec dks = new DESKeySpec(key);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey securekey = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(2, securekey, sr);
        return cipher.doFinal(src);
    }

    private static String byte2hex(byte[] b) {
        String hs = "";
        String stmp = "";

        for(int n = 0; n < b.length; ++n) {
            stmp = Integer.toHexString(b[n] & 255);
            if (stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
        }

        return hs.toUpperCase();
    }

    private static byte[] hex2byte(byte[] b) {
        if (b.length % 2 != 0) {
            throw new IllegalArgumentException("length not even");
        } else {
            byte[] b2 = new byte[b.length / 2];

            for(int n = 0; n < b.length; n += 2) {
                String item = new String(b, n, 2);
                b2[n / 2] = (byte)Integer.parseInt(item, 16);
            }

            return b2;
        }
    }

    public static String decode(String src, String key) {
        if (StringUtils.isEmpty(key)) {
            key = "kimwaynet";
        }

        String decryptStr = "";

        try {
            decryptStr = new String(decrypt(hex2byte(src.getBytes()), key.getBytes()));
        } catch (Exception var4) {
            var4.printStackTrace();
        }

        return decryptStr;
    }

    public static String encode(String src, String key) {
        if (StringUtils.isEmpty(key)) {
            key = "kimwaynet";
        }

        byte[] bytes = null;
        String encryptStr = "";

        try {
            bytes = encrypt(src.getBytes(), key.getBytes());
        } catch (Exception var5) {
            var5.printStackTrace();
        }

        if (bytes != null) {
            encryptStr = byte2hex(bytes);
        }

        return encryptStr;
    }

    public static String getDecodeMAC(String decryptStr) {
        String mac = "";
        String[] strs = decryptStr.split(":");
        if (strs.length == 5) {
            mac = strs[0];
        }

        return mac;
    }

    public static final String randomString(int length) {
        if (length < 1) {
            return null;
        } else {
            if (randGen == null) {
                randGen = new Random();
                numbersAndLetters = "0123456789abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
            }

            char[] randBuffer = new char[length];

            for(int i = 0; i < randBuffer.length; ++i) {
                randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];
            }
 
            return new String(randBuffer);
        }
    }

    public static void main(String[] args) {
        System.out.println(decode("5BFA5BA22C8F2B2E", "zhongjing"));
    }
}
